/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ipfms.manager;

import com.google.inject.Inject;
import com.wideplay.warp.persist.Transactional;

import ipfms.dao.InboxDAO;
import ipfms.model.Inbox;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author fikri
 */
public class InboxManager {

    @Inject
    protected com.google.inject.Provider<Session> sessionProvider;
    
    @Inject InboxDAO inboxDAO;

    


    public Inbox getTugasan (String idBuilding, String userId) {
        Session session = sessionProvider.get();
        Query query = session.createQuery(
                "select f from Inbox f " +
                "where f.building.idBuilding = :idBuilding and f.pengguna.idPengguna = :idPengguna");
        query.setString("buildingId", idBuilding);
        query.setString("idPengguna", userId);
        return (Inbox) query.uniqueResult();
    }

    @Transactional
    public void save (Inbox inbox) {
        inboxDAO.saveOrUpdate(inbox);
    }

    public List<Inbox> senaraiInbox (String idWorkflow, String userId) {
        Session session = sessionProvider.get();
        Query query = session.createQuery(
                "select f from Inbox f " +
                "where workflowId = :idAliran and ( pengguna.idPengguna = :idPengguna or pengguna.idPengguna is null) " +
                "and active='Y' order by f.infoAudit.tarikhMasuk asc");

        query.setString("idAliran", idWorkflow);
        query.setString("idPengguna", userId);

        return query.list();
    }

    public Inbox getWorkflow (String idWorkflow, String buildingId) {
        Session session = sessionProvider.get();
        Query query = session.createQuery(
                "select f from Inbox f " +
                "where workflowId = :idAliran and pengguna.idPengguna is null " +
                "and building.idBuilding = :buildingId " +
                "and active='Y'");

        query.setString("idAliran", idWorkflow);
        query.setString("buildingId", buildingId);

        return (Inbox)query.uniqueResult();
    }

    public Inbox getWorkflowInfo (String idWorkflow, String buildingId) {
        Session session = sessionProvider.get();
        Query query = session.createQuery(
                "select f from Inbox f " +                
                "where workflowId = :idAliran " +
                "and building.idBuilding = :buildingId ");

        query.setString("idAliran", idWorkflow);
        query.setString("buildingId", buildingId);

        return (Inbox)query.uniqueResult();
    }  
    
    public Inbox getInbox (String buildingId) {
        Session session = sessionProvider.get();
        Query query = session.createQuery(
                "select f from Inbox f " +                                
                "where building.idBuilding = :buildingId ");
        
        query.setString("buildingId", buildingId);

        return (Inbox)query.uniqueResult();
    }
}
